<!--
 * @Descripttion:
 * @version:
 * @Author: sueRimn
 * @Date: 2020-10-22 11:41:46
 * @LastEditors: sueRimn
 * @LastEditTime: 2020-11-04 15:56:02
-->
<template>
  <div class="app-container">

    <div class="content_box">
      <div class="leftActive">
        <el-menu :default-active="step" @select="handleSelect">
          <el-menu-item index="1">
            <span slot="title">欢迎页设置</span>
          </el-menu-item>
          <el-menu-item index="2" :disabled="step4IsShow">
            <span slot="title">问卷选择</span>
          </el-menu-item>
        </el-menu>
      </div>
      <div class="cententBody">
        <!-- 步骤1 -->
        <div v-show="step === '1'" class="left_from">
          <el-form ref="ruleForm" :model="form" :rules="rules" label-width="150px" label-position="left">
            <el-form-item label="调研/评测对象:">
              <el-radio-group v-model="form.face">
                <el-radio :label="'1'">学生</el-radio>
                <el-radio :label="'2'">家长</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="标题" prop="zoom_name">
              <el-input v-model="form.zoom_name" placeholder="请输入问卷标题" />
            </el-form-item>
            <el-form-item label="学校/机构简介:" prop="school_description">
              <el-input v-model="form.school_description" type="textarea" placeholder="请输入学校/机构简介" :rows="4" />
            </el-form-item>
            <el-form-item label="上传配图:">
              <upload ref="upload3" :config="config2" upload-id="uploadId5" @filepath="filepath2(arguments)" />
            </el-form-item>
            <div class="textTips">请上传宽高比例为16:9的图片,以达到最佳预览效果</div>
            <el-form-item label="上传视频:">
              <upload ref="upload4" :config="config3" upload-id="uploadId6" @filepath="filepath3(arguments)" />
            </el-form-item>
            <div class="textTips">请上传宽高比例为16:9的视频,以达到最佳预览效果</div>
            <el-form-item label="调研/评测内容简介:" prop="group_description">
              <el-input v-model="form.group_description" type="textarea" placeholder="请输入调研/评测内容简介" :rows="4" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit('ruleForm')">下一步</el-button>
              <el-button @click="yuLan('ruleForm')">预览</el-button>
            </el-form-item>
          </el-form>
        </div>
        <!-- 组卷 -->
        <div v-show="step == 2" style="width: 100%;">
          <test-paper :paper-data="form" />
        </div>
      </div>
      <div v-if="step === '1'" class="rightBox">
        <Phone :show_url="yuLanUrl" :ischangebtn="false" />
      </div>
    </div>
  </div>
</template>

<script>
import Phone from '@/components/Phone'
import upload from '@/components/upload/index'
import { uploadConfig2 } from '@/components/upload/config/upload1'
import { uploadConfig3 } from '@/components/upload/config/upload2'

import testPaper from '../paper.vue'

import { addPaper } from '@/api/paper'
const Global = require('../../../../env/index')

export default {
    name: 'ZoomAdd',
    components: {
        Phone,
        upload,
        testPaper
    },
    data() {
        return {
            // 上传图片参数
            config2: uploadConfig2,
            config3: uploadConfig3,
            filepath2li: [],
            filepath3li: [],
            step: '1', // 步骤
            // 欢迎页新增
            form: {
                face: '1',
                zoom_name: '',
                school_description: '',
                group_description: '',
                picture: '', // 图片
                video: '' // 视频
            },
            yuLanUrl: '', // 预览路径
            // 验证规则
            rules: {
                zoom_name: [{
                    required: true,
                    message: '请输入问卷标题',
                    trigger: 'blur'
                },
                {
                    min: 4,
                    message: '长度在 4 个字符以上',
                    trigger: 'blur'
                }
                ],
                school_description: [{
                    required: true,
                    message: '请输入学校/机构简介',
                    trigger: 'blur'
                },
                {
                    min: 4,
                    message: '长度在 4 个字符以上',
                    trigger: 'blur'
                }
                ],
                group_description: [{
                    required: true,
                    message: '请输入调研/评测内容简介',
                    trigger: 'blur'
                },
                {
                    min: 4,
                    message: '长度在 4 个字符以上',
                    trigger: 'blur'
                }
                ]
            }
        }
    },
    computed: {
        step4IsShow() {
            return this.step === '1' || this.step === '3'
        }
    },
    created() {
    // 为了方便，临时拉取数据，写完后腰删除
    // getEditQuestionInfos({
    //   group_id: this.form.unid
    // }).then(res => {
    //   this.questionList = res.data
    //   console.log('题目列表', this.questionList)
    // })
    // this.getQuestionList()
    },
    methods: {
    //   添加图片调用的方法
        filepath2(param) {
            console.log('上传图片', param[0])
            this.form.picture = param[1] + param[0]
            this.filepath2li = [{
                filepath: param[0]
            }]
        },
        //   添加视频调用的方法
        filepath3(param) {
            this.form.video = param[1] + param[0]
            this.filepath3li = [{
                filepath: param[0]
            }]
        },
        // 第一步创建欢迎页
        onSubmit(formName) {
            this.$refs[formName].validate(async valid => {
                if (valid) {
                    addPaper(this.form).then(async res => {
                        console.log(res)
                        if (res.code === 200) {
                            if (this.filepath2li.length != 0) {
                                await this.$refs.upload3.putFile(this.filepath2li)
                            }
                            if (this.filepath2li.length != 0) {
                                await this.$refs.upload4.putFile(this.filepath3li)
                            }
                            this.form = res.data
                            this.step = '2' // 下一步
                        } else {
                            this.$alert('创建失败，请重试', '提示', {
                                confirmButtonText: '确定',
                                callback: () => {}
                            })
                        }
                    })
                } else {
                    return false
                }
            })
        },
        // 预览
        yuLan(formName) {
            this.$refs[formName].validate(async valid => {
                if (valid) {
                    addPaper(this.form).then(async res => {
                        console.log(res)
                        if (res.code === 200) {
                            if (this.filepath2li.length != 0) {
                                await this.$refs.upload3.putFile(this.filepath2li)
                            }
                            if (this.filepath2li.length != 0) {
                                await this.$refs.upload4.putFile(this.filepath3li)
                            }
                            this.form = res.data
                            this.yuLanUrl = `${Global.ifram_url}?psq_test_id=${this.form.unid}&link_type=search&qstype=zoom&?rand=` + Math.round(Math.random() * 10000000)
                        } else {
                            this.$alert('预览失败，请重试', '提示', {
                                confirmButtonText: '确定',
                                callback: () => {}
                            })
                        }
                    })
                } else {
                    return false
                }
            })
        },

        // 选择步骤
        handleSelect(key) {
            this.step = key + ''
            console.log(this.step)
        }

    }
}
</script>

<style lang="scss" scoped>
.content_box{
    width: 100%;
    display: flex;

    .leftActive{
        flex-grow:0
    }

    .cententBody{
        flex-grow:1;
        padding: 0 40px;

        .left_from{
            min-width: 500px;
            max-width: 800px;
        }
        .textTips {
            font-size: 12px;
            color: #a2a4aa;
            padding-left: 150px;
            box-sizing: border-box;
            margin-bottom: 25px;
        }
    }

    .rightBox{
        flex-grow:0
    }
}

</style>
